<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="inet.db.tennis.TennisChampion"%>
<%@page import="inet.db.tennis.buffer.TennisChampionBuffer"%>
<%@page import="inet.db.tennis.buffer.TennisSeasonBuffer"%>
<jsp:useBean id="tennisSeasonDAO" class="inet.db.tennis.TennisSeasonDAO" scope="session"/>
<jsp:useBean id="tennisChampionDAO" class="inet.db.tennis.TennisChampionDAO" scope="session"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Thêm mới mùa giải</title>
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/reset.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/text.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/grid.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/layout.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/nav.css" media="screen" />
    <!--[if IE 6]><link rel="stylesheet" type="text/css" href="css/ie6.css" media="screen" /><![endif]-->
    <!--[if IE 7]><link rel="stylesheet" type="text/css" href="css/ie.css" media="screen" /><![endif]-->
    <link href="<%= request.getContextPath() %>/admin/tennis/css/fancy-button/fancy-button.css" rel="stylesheet" type="text/css" />
    <!--Jquery UI CSS-->
    <link href="<%= request.getContextPath() %>/admin/tennis/css/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />
    <!-- BEGIN: load jquery -->
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.core.min.js"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.widget.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.accordion.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.effects.core.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.effects.slide.min.js" type="text/javascript"></script>
    <!-- END: load jquery -->
    
    <!--Fancy Button-->
    <script src="<%= request.getContextPath() %>/admin/tennis/js/fancy-button/fancy-button.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/setup.js" type="text/javascript"></script>
    <script type="text/javascript">
    	function f_add_season() {
    		if(document.formAddSeason.txtName.value==''){
				document.getElementById("lblName").innerHTML = 'Nhập tên mùa giải.';
				document.formAddSeason.txtName.focus();
				return false;
			}else{
				document.getElementById("lblName").innerHTML = '';
			}
    		document.formAddSeason.action.value = "CREATE";
    		document.formAddSeason.submit();
    	}
    	function disableEnterKey(e)
		{
		     var key;
		     if(window.event)
		          key = window.event.keyCode;//IE
		     else
		          key = e.which;//firefox
		     if(key == 13)
		        f_add_season();
		}
    </script>
</head>
<body>
    <div class="container_12">
        <%@ include file="../include/header.jsp" %>
        <% 
        	// set Character Encoding before any getParameter() methods.
		    request.setCharacterEncoding("UTF-8");
		    response.setCharacterEncoding("UTF-8");
		    String sAction = request.getParameter("action");
		    String sChampionCode = request.getParameter("championCode");
		    String sName = request.getParameter("txtName");
		    String sMessageErr = "";
		    String sMessageOk = "";
		    String sMessageWarning = "";
		    if(sChampionCode == null)sChampionCode ="";
		    
		    TennisChampion champion = tennisChampionDAO.getRow(sChampionCode);
		    if(champion == null){
		    	out.println("Không tìm thấy giải đấu có mã "+sChampionCode);
		    	return;
		    }
		    if(sAction != null && "CREATE".equals(sAction)) {
		    	if(sName == null || "".equals(sName) ) {
		    		sMessageWarning = "Bạn phải nhập tên mùa giải";
		    	} else {
		    		boolean isOk = tennisSeasonDAO.insertRow(sChampionCode, sName);
		    		if(isOk) {
		    			TennisSeasonBuffer.reload();
		    			sMessageOk = "Thêm mùa giải thành công";
		    		} else {
		    			sMessageErr = "Xảy ra lỗi khi thêm mùa giải";
		    		}
		    	}
		    }
        %>
        <div class="grid_5">
            <div class="box round first fullpage">
                <h2>Thêm mùa giải</h2>
                <div class="block ">
                    <form name="formAddSeason" onKeyPress="return disableEnterKey(event)" method="post">
                    	<input type="hidden" name="action"/>
                    	<% if( !"".equals(sMessageWarning) ) { %>
                    	<div class="message warning">
                            <h5>Cảnh báo !</h5>
                            <p>
                                <%= sMessageErr %>
                            </p>
                        </div>
                        <% } %>
                    	<% if( !"".equals(sMessageOk) ) { %>
                    	<div class="message success">
                            <h5>Success!</h5>
                            <p>
                                <%= sMessageOk %>
                            </p>
                        </div>
                        <% } %>
                    	<% if( !"".equals(sMessageErr) ) { %>
                    	<div class="message error">
                            <h5>Error!</h5>
                            <p>
                                <%= sMessageErr %>
                            </p>
                        </div>
                        <% } %>
	                    <table class="form">
	                        <tr>
	                            <td class="col1">
	                                <label>Tên mùa giải</label>
	                            </td>
	                            <td class="col2">
	                                <input type="text" id="grumble" name="txtName"/>
	                                <span id="lblName" class="error"></span>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Giải đấu</label>
	                            </td>
	                            <td>
	                                <%= champion.getName() %>
	                            </td>
	                        </tr>
	                        <tr>
	                        	<td>
	                                <label>&nbsp;</label>
	                            </td>
	                            <td>
	                            	<button type="button" onclick="f_add_season();" class="btn btn-grey">Thêm mới</button>
	                            	<button type="button" onclick="window.location.href='list-season.jsp?championCode=<%= sChampionCode %>';" class="btn btn-grey">Quay lại</button>
	                            </td>
	                        </tr>
	                    </table>
                    </form>
                </div>
            </div>
        </div>
        <div class="clear">
        </div>
    </div>
    <%@ include file="../include/footer.jsp" %>
</body>
</html>